package com.gxa.aspect;

import com.gxa.pojo.Administrators;
import com.gxa.pojo.Log;
import com.gxa.service.LogService;
import com.gxa.utils.IPUtils;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 *
 * @author bq
 * Date: 2020/10/16 19:55
 * Description:
 * Version: V1.0
 */
@Aspect
@Component
public class LogAspect {
    @Autowired
    private LogService logService;
    @Autowired
    private Log log;
    private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(LogAspect.class);
    @Before("execution(* com.gxa.service.*.*(..))")
    public void doBefore(JoinPoint point){
        logger.info("["+point.getKind()+"]方法执行前,当前时间："+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
//         记录下请求内容
        logger.info("["+point.getKind()+"]请求URL : " + request.getRequestURL().toString());
        logger.info("["+point.getKind()+"]请求方法 : " + request.getMethod());
        logger.info("["+point.getKind()+"]IP地址 : " + request.getRemoteAddr());
    }
    //定义通知，方法执行后
    @After("execution(* com.gxa.service.*.*(..))")
    public void after(JoinPoint point){
        logger.info("["+point.getKind()+"]方法执行后,当前时间："+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String adminUsername = ((Administrators) SecurityUtils.getSubject().getPrincipal()).getAdminUsername();
        log.setOperator(adminUsername);
        log.setIpAdd(IPUtils.getIpAddr(request));
        log.setOperaRec(point.getKind());
        logService.addLog(log);
    }
    //定义通知，方法返回前
    @AfterReturning("execution(* com.gxa.service.*.*(..))")
    public void AfterReturning(JoinPoint point){
        logger.info("["+point.getKind()+"]方法返回前,当前时间："+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }
    //定义通知，抛出异常
    @AfterThrowing(value = "execution(* com.gxa.service.*.*(..))",throwing = "error")
    public void AfterThrowing(Throwable error){
        logger.info("方法报错,当前时间："+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        logger.error("actionDetailBiz max monitorTime upload data error,",error);
    }
    //定义通知环绕型
    @Around("execution(* com.gxa.service.*.*(..))")
    public Object Around (ProceedingJoinPoint pjp) throws Throwable{
        logger.info("["+pjp.getKind()+"]环绕前:"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        Object obj= pjp.proceed();
        logger.info("["+pjp.getKind()+"]环绕后:"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return obj;
    }
}
